const initState = {
  count: 200,
  num: 0,
};

// const state = JSON.parse(localStorage.getItem("count_vuex")) || initState;
const state = JSON.parse(localStorage.getItem("count_vuex")) || initState;

const mutations = {
  /* mutations的方法会接受state作为第一个参数 */
  increase(state) {
    state.count++;
    // console.log(state);

    // localStorage.setItem("count_vuex", JSON.stringify(state));
    localStorage.setItem("count_vuex", JSON.stringify(state));
  },
  increaseN(state, payload) {
    state.count += payload.n;
    localStorage.setItem("count_vuex", JSON.stringify(state));
  },
};
const actions = {
  // 默认参数是store 把commit解构出来
  increaseWait({ commit }) {
    setTimeout(() => {
      commit("increase");
    }, 1000);
  },
  increaseNWait({ commit }, payload) {
    setTimeout(() => {
      commit("increaseN", payload);
    }, 1000);
  },
};
const getters = {
  countSqrt(state) {
    return Math.sqrt(state.count);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
